package com.block.datapush.form.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.model.dto.CountInfoDTO;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushResultEnum;
import com.block.datapush.form.service.IFormInfoHisService;
import com.block.datapush.form.service.IFormInfoProfitService;
import com.block.datapush.model.dto.FormItemDTO;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.FormInfoProfit;
import com.block.db.entity.MemberExtInfo;
import com.block.db.mapper.FormInfoHisMapper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
public class FormInfoHisServiceImpl extends ServiceImpl<FormInfoHisMapper, FormInfoHis> implements IFormInfoHisService {

    private static Logger log = LogFactory.getCommLog();

    @Autowired
    IFormInfoProfitService formInfoProfitService;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public FormInfoHis initFormInfoHis(FormInfo formInfo, MemberExtInfo memberExtInfo, String pushType){

        FormInfoHis formInfoHis = new FormInfoHis();

        formInfoHis.setUid(memberExtInfo.getUid());
        formInfoHis.setMobile(memberExtInfo.getMobile());
        formInfoHis.setUsername(memberExtInfo.getUsername());
        formInfoHis.setUserCreateTime(memberExtInfo.getCreateTime());
        formInfoHis.setChannelCode(memberExtInfo.getChannelCode());
        formInfoHis.setFormId(formInfo.getId());
        formInfoHis.setDateInfo(LocalDate.now().toString());
        formInfoHis.setChannelName(formInfo.getChannelName());
        formInfoHis.setPushResult(PushResultEnum.INIT.getType());
        formInfoHis.setPushType(pushType);

//        formInfoHis.setPriceAmount(String.valueOf(formItemDTO.getPriceAmount()));
//        formInfoHis.setItemId(String.valueOf(formItemDTO.getItemId()));

        boolean save = this.save(formInfoHis);
        // 初始化收益信息
        boolean initRes = formInfoProfitService.initProfitInfo(formInfo);

        if (save && initRes){
            return formInfoHis;
        }

        throw new RuntimeException("");
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public boolean updateFormInfoHis(FormInfoHis formInfoHis, PushResultEnum pushResultEnum, FormPushResultDTO pushResultDTO, FormItemDTO formItemDTO){

        FormInfoHis updateHis = new FormInfoHis();
        updateHis.setId(formInfoHis.getId());

        FormInfoProfit updateProfit = new FormInfoProfit();
        updateProfit.setDateInfo(formInfoHis.getDateInfo());
        updateProfit.setInfoId(formInfoHis.getFormId());

        if (StrUtil.equals(pushResultEnum.getType(), PushResultEnum.FILTER_ERROR.getType())){
            // 过筛失败
            updateProfit.setFilterErrorNumber(1);

            updateHis.setPushResult(pushResultEnum.getType());
            updateHis.setRetMsg(pushResultEnum.getName());

        }else if (StrUtil.equals(pushResultEnum.getType(), PushResultEnum.RISK_ERROR.getType())){
            // 过筛失败
            updateProfit.setFilterErrorNumber(1);

            updateHis.setPushResult(pushResultEnum.getType());
            updateHis.setRetMsg(pushResultEnum.getName());

        }else if (StrUtil.equals(pushResultEnum.getType(), PushResultEnum.MATCH_ERROR.getType())){
            updateProfit.setMatchErrorNumber(1);

            updateHis.setPushResult(pushResultEnum.getType());
            updateHis.setRetMsg(pushResultEnum.getName());

        }else if (StrUtil.equals(pushResultEnum.getType(), PushResultEnum.WAIT_CONFIRM.getType())){
            updateProfit.setWaitConfirmNumber(1);

            updateHis.setPushResult(pushResultEnum.getType());
            if (ObjectUtil.isNotEmpty(formItemDTO)){
                updateProfit.setProfitAmount(formItemDTO.getPriceAmount());
                updateProfit.setAvgAmount(formItemDTO.getPriceAmount());

                updateHis.setPriceAmount(String.valueOf(formItemDTO.getPriceAmount()));
                updateHis.setItemId(String.valueOf(formItemDTO.getItemId()));
            }

            if (ObjectUtil.isNotEmpty(pushResultDTO)) {
                updateHis.setRetCode(pushResultDTO.getCode() + "|" + pushResultDTO.getMessage());
                updateHis.setRetMsg(pushResultDTO.getResult());
                updateHis.setPushKey(pushResultDTO.getParamContent());
                // 在推送方法中确认推送结果
                updateHis.setOutOrderNo(pushResultDTO.getOutOrderNo());
            }

        }else if (StrUtil.equals(pushResultEnum.getType(), PushResultEnum.SUCCESS.getType())){
            updateProfit.setPushSuccessNumber(1);


            if (ObjectUtil.isNotEmpty(formItemDTO)){
                updateProfit.setProfitAmount(formItemDTO.getPriceAmount());
                updateProfit.setAvgAmount(formItemDTO.getPriceAmount());

                updateHis.setPriceAmount(String.valueOf(formItemDTO.getPriceAmount()));
                updateHis.setItemId(String.valueOf(formItemDTO.getItemId()));
            }

            updateHis.setPushResult(pushResultEnum.getType());
            if (ObjectUtil.isNotEmpty(pushResultDTO)) {
                updateHis.setRetCode(pushResultDTO.getCode() + "|" + pushResultDTO.getMessage());
                updateHis.setRetMsg(pushResultDTO.getResult());
                updateHis.setPushKey(pushResultDTO.getParamContent());
                // 在推送方法中确认推送结果
                updateHis.setOutOrderNo(pushResultDTO.getOutOrderNo());
            }

        }else if (StrUtil.equals(pushResultEnum.getType(), PushResultEnum.FAILED.getType())){
            updateProfit.setPushErrorNumber(1);

            updateHis.setPushResult(pushResultEnum.getType());
            updateHis.setRetCode(pushResultDTO.getCode() + "|" + pushResultDTO.getMessage());
            updateHis.setRetMsg(pushResultDTO.getResult());
            updateHis.setPushKey(pushResultDTO.getParamContent());

        }else if (StrUtil.equals(pushResultEnum.getType(), PushResultEnum.TIMEOUT.getType())){
            updateProfit.setTimeoutNumber(1);
            updateHis.setPushResult(pushResultEnum.getType());
            updateHis.setRetMsg(pushResultEnum.getName());
        }

        boolean resultHis =  this.updateById(updateHis);
        boolean resultProfit =  formInfoProfitService.updateProfitInfo(updateProfit);

        return resultHis && resultProfit;
    }

}
